<template>
  <div>
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input v-model="params.enterprise_name" placeholder="企业名称查询" clearable>
          <i class="el-icon-search el-input__icon" slot="suffix" @click="getData()"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <c-select
          :options="enumhandleStatus"
          placeholder="审批状态"
          v-model="params['handle_status.eq']"
          @change="getData()"
          clearable
        ></c-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
      <div class="text-muted">
        <div>合计：</div>
        项目总数：
        <span class="text-primary">{{ totalCount || '-' }}</span>
      </div>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData"></c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <approve-dialog
      :visible.sync="approveVisibe"
      :query-id="queryId"
      :bus-id="busId"
      :form-data="formData"
      :view-mode="viewMode"
      @success="getData()"
    ></approve-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import ApproveDialog from '@/bigcredit/views/letter/claim/components/ApproveDialog'
export const enumhandleStatus = {
  0: '待审核',
  1: '已通过',
  2: '已拒绝',
}
export default {
  components: {
    ApproveDialog,
  },
  props: {},
  data() {
    return {
      enumhandleStatus,
      loading: false,
      tableName: 'guarantee_claim_view',
      tableData: [],
      tableColumns: [
        { attrs: { label: '序号', prop: 'index', align: 'center' } },
        { attrs: { label: '索赔人名称', prop: 'claimer_name', align: 'center' } },
        { attrs: { label: '保函编号', prop: 'guarantee_no', align: 'center' } },
        { attrs: { label: '业务申请码', prop: 'apply_no', align: 'center' } },
        { attrs: { label: '申请时间', prop: 'create_time', align: 'center', width: 100 } },
        { attrs: { label: '索赔金额(元)', prop: 'amount', align: 'center' } },
        { attrs: { label: '索赔原因', prop: 'claim_reason', align: 'center' } },
        { attrs: { label: '审批状态', prop: 'handleStatus', align: 'center' } },
        {
          attrs: { label: '操作', align: 'center', width: 100, fixed: 'right' },
          operations: [
            {
              content: '审核',
              icon: 'fa fa-legal',
              event: this.handleEdit,
              ifRender: row => this.hasClaimAuth && +(row.handle_status || '') === 0,
            },
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.handleView,
            },
          ],
        },
      ],
      totalCount: 0,
      params: {
        index: 1,
        size: 15,
      },
      approveVisibe: false,
      queryId: '',
      busId: '',
      formData: {},
      viewMode: false,
    }
  },
  computed: {
    ...mapGetters(['clientId', 'user']),
    hasClaimAuth() {
      return this.user.scopeArr.includes('bhClaim')
    },
  },
  watch: {},
  created() {
    this.params['service_id.eq'] = this.clientId
    this.getData()
  },
  mounted() {},
  methods: {
    handleView(row = {}) {
      this.approveVisibe = true
      this.viewMode = true
      this.formData = { ...row }
      this.queryId = row?.customer_related_id ?? ''
      this.busId = row?.bus_id ?? ''
    },
    handleEdit(row = {}) {
      this.approveVisibe = true
      this.viewMode = false
      this.formData = { ...row }
      this.queryId = row?.customer_related_id ?? ''
      this.busId = row?.bus_id ?? ''
    },
    getData() {
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params, { create_time: 'desc' })
        .then(res => {
          const data = res?.data ?? {}
          this.totalCount = data.totalCount ?? 0
          this.tableData = (data.data ?? []).map(this.dataAdapter)
        })
        .finally(() => {
          this.loading = false
        })
    },
    dataAdapter(item, index) {
      item.index = this.totalCount - index - this.params.size * (this.params.index - 1)
      item.handleStatus = enumhandleStatus[item.handle_status]
      return item
    },
  },
}
</script>

<style lang="scss" scoped></style>
